package com.neuedu.weishi.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.neuedu.weishi.util.FileUtils;
import com.neuedu.weishi.util.StringUtils;
import com.neuedu.weishi.vo.ResponseData;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;

/**
 * 视频、图片文件上传控制器
 */
@RestController
@RequestMapping("/weishi/upload/api")
@Log4j2
public class UploadController {


    /**
     * 上传视频文件所存放的服务器本地目录
     */
    @Value("${file.upload.video.location}")
    private String videoLocationPath;
    /**
     * 上传图片文件所存放的服务器本地目录
     */
    @Value("${file.upload.img.location}")
    private String imgLocationPath;
    /**
     * 上传视频文件URL前缀
     */
    @Value("${file.upload.video.prefix}")
    private String videoUrlPrefix;
    /**
     * 上传图片文件URL前缀
     */
    @Value("${file.upload.img.prefix}")
    private   String imgUrlPrefix ;


    /**
     * 上传视频
     *
     * @param videoFile
     * @return
     */
    @PostMapping("/video")
    public ResponseData uploadVideo(@RequestParam("videoFile") MultipartFile videoFile) {
        if (videoFile.isEmpty()) {
            ResponseData.fail(999, "上传失败,未上传任何视频文件");
        }
        //原始文件名称
        String originFileName = videoFile.getOriginalFilename();
        if (originFileName == null) {
            return ResponseData.fail(999, "无法获取文件名");
        }
        //文本扩展名
        String suffix = originFileName.substring(originFileName.lastIndexOf('.'));
        if(!FileUtils.isVideoFile(originFileName)){
           return ResponseData.fail(999, "文件格式错误,请选择视频文件");
        }
        String newFileName = StringUtils.getUUID32() + suffix;
        log.info(newFileName);
        //上传文件所在父目录,名称两位的年+三位年中的天,也可以自定义规则
        String parentName= DateUtil.format(new Date(),"yyDDD");
        File parent=new File(videoLocationPath,parentName);
        FileUtil.mkParentDirs(parent);
        FileUtil.mkdir(parent);
        File dest = new File(parent, newFileName);
        try {
            videoFile.transferTo(dest);
            //视频URL
            String videoUrl= videoUrlPrefix.replace("**","")+parentName + '/' + newFileName;
            return ResponseData.success(videoUrl);
        } catch (IOException e) {
            log.error(e.toString(), e);
            return ResponseData.fail(500, e.toString());
        }
    }
    /**
     * 上传会员头像
     *
     * @param imgFile
     * @return
     */
    @PostMapping("/member/head")
    public ResponseData uploadHead(@RequestParam("head") MultipartFile imgFile,int userId) {
        if (imgFile.isEmpty()) {
            ResponseData.fail(999, "上传失败,未上传任何图片文件");
        }

        //原始文件名称
        String originFileName = imgFile.getOriginalFilename();
        if (originFileName == null) {
            return ResponseData.fail(999, "无法获取文件名");
        }
        //文本扩展名
        String suffix = originFileName.substring(originFileName.lastIndexOf('.'));
        if(!FileUtils.isImgFile(originFileName)){
            return ResponseData.fail(999, "文件格式错误,请选择图片文件");
        }
        String newFileName = StringUtils.getUUID32() + suffix;
        log.info(newFileName);
        //上传文件所在父目录,head+用户ID
        String parentName= "head/"+userId;
        File parent=new File(imgLocationPath,parentName);
        FileUtil.mkParentDirs(parent);
        FileUtil.mkdir(parent);
        File dest = new File(parent, newFileName);
        try {
            imgFile.transferTo(dest);
            //图片URL
            String imgUrl= imgUrlPrefix.replace("**","")+parentName + '/' + newFileName;
            return ResponseData.success(imgUrl);
        }
        catch (IOException e) {
            log.error(e.toString(), e);
            return ResponseData.fail(500, e.toString());

        }
    }
}
